<%--
  Created by IntelliJ IDEA.
  User: 86157
  Date: 2022/1/9
  Time: 19:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>产品生产工序设计单审核</title>
</head>
<body>
<div id="lxm_shtable">
    <table id="designtal">

    </table>
</div>

<div id="lxm_shtable1" style="display: none">
    <div style="position: absolute; right: 10%">
        <input type="radio" form="design_ck_form" name="checkTag" value="S001-2" checked>不通过
        <input type="radio" form="design_ck_form" name="checkTag" value="S001-1">通过
        <input type="button" value="确定" class="btn btn-primary" id="btndodesignadd">
        <input type="button" value="返回" class="btn btn-primary" id="fhsh">
    </div><br><br>
    <form id="design_ck_form" class="form form-horizontal">
            <h4 align="center">生产工序设计单</h4><br>
        <div class="row">

            <div class="form-group col-md-6">
                <label class="control-label col-md-4">设计编号 </label>
                <div class="col-md-8">
                    <input type="text" class="form-control" name="designId" id="designId" readonly="readonly">
                </div>
            </div>

            <div class="form-group col-md-6">
                <label class="control-label col-md-4">设计人 </label>
                <div class="col-md-8">
                    <input type="text" class="form-control" name="designer" id="designer">
                </div>
            </div>
        </div>

        <div class="row">
            <div class="form-group col-md-6">
                <label class="control-label col-md-4">产品名称 </label>
                <div class="col-md-8">

                    <input type="text" class="form-control" name="productName" id="productName" readonly="readonly">
                    </select>
                </div>
            </div>
            <div class="form-group col-md-6">
                <label class="control-label col-md-4">产品编号 </label>
                <div class="col-md-8">
                    <input type="text" class="form-control" name="productId" id="productId" readonly="readonly">

                </div>
            </div>
        </div>

        <div class="row" align="center">
            <table id="shdesigntal" style="width: 1000px;height: 60px;" >

            </table>
        </div>
        <br>
        <div class="row">
            <div class="form-group col-md-6">
                <label class="control-label col-md-4">工时总成本 </label>
                <div class="col-md-8">

                    <input type="text" class="form-control" name="costPriceSum" id="costPriceSum" readonly="readonly">
                    </select>
                </div>
            </div>

        </div>

        <div class="row">
            <div class="form-group col-md-6">
                <label class="control-label col-md-4">审核人 </label>
                <div class="col-md-8">

                    <input type="text" class="form-control" name="checker" id="checker">
                    </select>
                </div>
            </div>
            <div class="form-group col-md-6">
                <label class="control-label col-md-4" id="djtime">审核时间 </label>
                <div class="col-md-8">
                    <input type="text" class="form-control" id="checkTime">

                </div>
            </div>
        </div>

        <div class="row">
            <div class="form-group col-md-12">
                <label class="control-label col-md-2">设计要求 </label>
                <div class="col-md-10">
                    <input type="text" class="form-control" name="procedureDescribe" style="height: 50px;width: 500px;">
                    </select>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
    var params_shenghe= {
        url: "${pageContext.request.contextPath}/queryalldesign.action",
        method: 'GET',                      //请求方式（*）
        //toolbar: '#toolbar',              //工具按钮用哪个容器
        striped: true,                      //是否显示行间隔色
        cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pagination: true,                   //是否显示分页（*）
        sortable: true,                     //是否启用排序
        sortOrder: "asc",                   //排序方式
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
        pageSize: 5,                     //每页的记录行数（*）
        pageList: [5, 10, 25, 50, 100],        //可供选择的每页的行数（*）
        search: false,                      //是否显示表格搜索
        strictSearch: true,
        showColumns: true,                  //是否显示所有的列（选择显示的列）
        showRefresh: true,                  //是否显示刷新按钮
        minimumCountColumns: 2,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        height: 500,                      //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
        uniqueId: "productId",            //每一行的唯一标识，一般为主键列
        showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
        cardView: false,                    //是否显示详细视图
        detailView: false,                  //是否显示父子表
        //传递额外的查询用的参数
        queryParams: function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var temp = {
                pagesize: params.limit,                         //页面大小
                pageno: (params.offset / params.limit) + 1,   //页码
                checkTag: "S001-0" //查询条件  等待审核
            };
            return temp;
        },
        columns: [
            {
                title: "设计单编号",
                field: "designId"
            },
            {
                title: "产品编号",
                field: "productId"
            },
            {
                title: "产品名称",
                field: "productName"
            },
            {
                title: "设计人",
                field: "designer",

            },
            {
                title: "登记时间",
                field: "registerTime"
            },
            {
                title: "工序总成本",
                field: "costPriceSum"
            },
            {
                title: "审核",
                field: "",
                formatter: function (value, row, index) {
                    return "<input type='button' onclick='shenghe(" + row.designId + ")' value='审核' class='btn btn-primary'>" ;
                }
            }
        ]
    };
    $("#designtal").bootstrapTable(params_shenghe);

    //工时成本小计
    $("#shdesigntal").on("change", function (e) {

        if (e.target.nodeName.toUpperCase() == "INPUT" && $(e.target).attr("type") == "text") {

            var str = $(e.target).attr("data-id");
            var index =parseInt( $(e.target).attr("data-id").substr(6));
            if(str.indexOf("amount")!=-1)
            {
                var val = e.target.value;

                var price = $(e.target).parent().next().next().find("input").val();

                $("#span" + index).text(val * price);
            }
            else  if(str.indexOf("cprice") !=-1)
            {
                var val = e.target.value;

                var amount = $(e.target).parent().prev().prev().find("input").val();

                $("#span" + index).text(val * amount);
            }
        }
    })
    
    function shenghe(designId) {
        $.get("${pageContext.request.contextPath}/querydesignbydid.action",{id:designId},function (data) {
            for (var key in data) {
                $("#" + key).val(data[key]);
            }

            //页面显示审核时间
            var dDate = new Date();
            var sDate = dDate.getFullYear() + "/" + (dDate.getMonth() + 1) + "/" + dDate.getDate();
            sDate = sDate + " " + dDate.getHours() + ":" + dDate.getMinutes() + ":" + dDate.getSeconds();
            $("#checkTime").val(sDate);

            //表格数据加载
            var paramsshenghe= {
                url:"${pageContext.request.contextPath}/querydesignDetailsbydid.action",
                striped: true,                      //是否显示行间隔色
                showRefresh: true,                  //是否显示刷新按钮
                clickToSelect: true,                //是否启用点击选中行
                height: 300,                      //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "procedureId",                     //每一行的唯一标识，一般为主键列
                //传递额外的查询用的参数
                queryParams: function (params) {
                    //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    var temp = {
                        id: data.id  //查询条件
                    };
                    return temp;
                },
                columns: [
                    {
                        title: "工序名称",
                        field: "procedureName"
                    },
                    {
                        title: "工序编号",
                        field: "procedureId"
                    },
                    {
                        title: "描述",
                        field: "procedureDescribe"
                    },
                    {
                        title: "工时数",
                        field: "labourHourAmount",
                        formatter: function (value, row, index) {
                            return "<input data-id='amount" + index + "'  type='text' class='form-control' value='"+value+"'/>"
                        }
                    },
                    {
                        title: "工时单位",
                        field: "amountUnit",
                        formatter: function (value, row, index) {
                            return "<input data-id='amunit" + index + "'  type='text' class='form-control' value='"+value+"'/>"
                        }
                    },
                    {
                        title: "单位工时成本",
                        field: "costPrice",
                        formatter: function (value, row, index) {
                            return "<input data-id='cprice" + index + "'  type='text' class='form-control' value='"+value+"'/>"
                        }
                    },

                    {
                        title: "工时成本小计（元）",
                        field: "subtotal",
                        formatter: function (value, row, index) {
                            return "<span id='span" + index + "'>"+value+"</span>";
                        }
                    }
                ]
            };
            $("#shdesigntal").bootstrapTable(paramsshenghe);
        }, "json")

        $("#lxm_shtable").attr("style","display:none")
        $("#lxm_shtable1").attr("style","display:block")
    }

    $("#fhsh").click( function () {
        $("#lxm_shtable").attr("style","display:block");
        $("#lxm_shtable1").attr("style","display:none");
    })

    $("#btndodesignadd").click(function () {

        //组装 工序设计表数据
        var design = $("#design_ck_form").serializeObject();

        //组装物料设计明细表数据
        var designDetails = [];
        var input_amount = $("#shdesigntal input[data-id^='amount']");
        var input_amunit = $("#shdesigntal input[data-id^='amunit']");
        var input_cprice = $("#shdesigntal input[data-id^='cprice']");


        //获取工序明细数据
        rows =$("#shdesigntal").bootstrapTable("getData");

        $.each(rows, function (index, item) {
            //productId  amount
            var dd = {id:item.id,
                procedureId: item.procedureId,
                labourHourAmount:input_amount.eq(index).val(),
                amountUnit: input_amunit.eq(index).val(),
                costPrice:input_cprice.eq(index).val()};
            designDetails.push(dd);
        });
        design["details"] = designDetails;

        $.ajax({
            url: "${pageContext.request.contextPath}/shdesign.action",
            type: "put",
            data: JSON.stringify(design),
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                if (data.code == 1) {
                    alert(data.msg)
                    $("#designtal").bootstrapTable("refresh")
                    $("#shdesigntal").bootstrapTable("refresh")

                    $("#lxm_shtable1").attr("style","display:none")
                    $("#lxm_shtable").attr("style","display:block")
                } else {
                    alert(data.msg)
                }
            }
        })


    })
</script>
</body>
</html>
